package jobs;

// 命令接口
interface Command {
    void execute();
}

// 设备：电灯
class Light {
    public void on() {
        System.out.println("电灯已打开");
    }
    public void off() {
        System.out.println("电灯已关闭");
    }
}

// 设备：空调
class AirConditioner {
    public void on() {
        System.out.println("空调已打开");
    }
    public void off() {
        System.out.println("空调已关闭");
    }
}

// 具体命令：打开电灯
class LightOnCommand implements Command {
    private Light light;
    public LightOnCommand(Light light) {
        this.light = light;
    }
    @Override
    public void execute() {
        light.on();
    }
}

// 具体命令：关闭电灯
class LightOffCommand implements Command {
    private Light light;
    public LightOffCommand(Light light) {
        this.light = light;
    }
    @Override
    public void execute() {
        light.off();
    }
}

// 具体命令：打开空调
class AirConditionerOnCommand implements Command {
    private AirConditioner ac;
    public AirConditionerOnCommand(AirConditioner ac) {
        this.ac = ac;
    }
    @Override
    public void execute() {
        ac.on();
    }
}

// 具体命令：关闭空调
class AirConditionerOffCommand implements Command {
    private AirConditioner ac;
    public AirConditionerOffCommand(AirConditioner ac) {
        this.ac = ac;
    }
    @Override
    public void execute() {
        ac.off();
    }
}

// 调用者
class RemoteControl {
    private Command command;
    public void setCommand(Command command) {
        this.command = command;
    }
    public void pressButton() {
        if (command != null) {
            command.execute();
        }
    }
}

// 测试类
public class SmartHomeCommandTest {
    public static void main(String[] args) {
        Light light = new Light();
        AirConditioner ac = new AirConditioner();
        RemoteControl remote = new RemoteControl();

        // 打开电灯
        remote.setCommand(new LightOnCommand(light));
        remote.pressButton();
        // 关闭电灯
        remote.setCommand(new LightOffCommand(light));
        remote.pressButton();
        // 打开空调
        remote.setCommand(new AirConditionerOnCommand(ac));
        remote.pressButton();
        // 关闭空调
        remote.setCommand(new AirConditionerOffCommand(ac));
        remote.pressButton();
    }
}
